package ecustom.finance.bean;

import ecustom.clients.k3client.K3WebApiClient;
import ecustom.clients.k3client.entities.ERPPAYBILLReq.FPAYBILLENTRY;
import ecustom.clients.k3client.entities.ERPPPayableReq;
import ecustom.clients.k3client.entities.ERPPPayableReq.FEntityDetail;
import ecustom.clients.k3client.entities.ERPPPayableReq.FsubHeadFinc;
import ecustom.clients.k3client.entities.ERPPPayableReq.FsubHeadSuppiler;
import ecustom.clients.util.JsonUtil;
import ecustom.common.CustomMap;
import ecustom.common.servicelog.bean.ServiceLogBean;
import ecustom.common.util.CustomUtil;
import ecustom.finance.actions.FYLFKAction;
import weaver.file.Prop;
import weaver.soa.workflow.request.DetailTable;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;
import weaver.soa.workflow.request.Row;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* @author 作者:Gcc
* @createDate 创建时间：Aug 24, 2023 2:40:32 PM
*/
public class SavePayableBean extends FYLFKAction{
	
	public Map<String, Object> handlERPData(RequestInfo requestInfo, Property[] mainFields) throws Exception {
		ServiceLogBean logBean = new ServiceLogBean("FYLFKAction", "ecustom.finance.actions.FYLFKAction-费用类付款-保存费用应付单",  "K3Cloud", "", System.currentTimeMillis());
		String formid = Prop.getPropValue("ecustom", "k3.yfd.formid");
		
		ERPPPayableReq req = setAYBILLReq(requestInfo, mainFields);
		log.info("outReq"+JsonUtil.toJson(req));
		Map<String, Object> erpResult = K3WebApiClient.saveData(logBean, req, formid);
		return erpResult;
	}
	
	public ERPPPayableReq setAYBILLReq(RequestInfo requestInfo, Property[] mainFields) throws Exception, Exception {
		String fqzz = FinanceBean.getERPOrgNumber(super.getMainStringCK(mainFields, "fqzz", "发起组织不能为空！"));	// 所属公司
		String erpOrgNumber = super.getMainStringCK(mainFields, "ERPORGNUMBER", "ERP使用组织编码不能为空！");	// ERP使用组织编码
		String gys = FinanceBean.getERPGys(super.getMainStringCK(mainFields, "gys", "供应商不能为空！"), erpOrgNumber);	// 供应商
		String bb = super.getMainStringCK(mainFields, "bb", "币别不能为空！");	// 币别
		String lzlx = super.getMainStringCK(mainFields, "lzlx", "立账类型不能为空！");	// 立账类型
		String cgbm = FinanceBean.getERPDeptNumberV2(super.getMainString(mainFields, "cgbm"));	// 采购部门
		String fyzy = super.getMainString(mainFields, "fyzy");	// 费用摘要
		
		ERPPPayableReq repPUR = new ERPPPayableReq();
		repPUR.setIsAutoSubmitAndAudit("true");	// 审核
		repPUR.getModel().setFID(0);	// 实体主键，有实体主键为修改操作，没有则新建
		repPUR.getModel().getFBillTypeID().put("FNUMBER", "YFD02_SYS");	// 固定值
		repPUR.getModel().setFBUSINESSTYPE("FY");
		repPUR.getModel().setFDATE(CustomUtil.getStringDate());	// 单据时间
		repPUR.getModel().setFENDDATE_H(CustomUtil.getStringDate());	// 到期日
		repPUR.getModel().getFSUPPLIERID().put("FNumber", gys);	// 供应商
		repPUR.getModel().getFCURRENCYID().put("FNumber", bb);	// 币别
		repPUR.getModel().setFISPRICEEXCLUDETAX(true);	// 价外税
		repPUR.getModel().setFISTAX(true);	// 按含税单价录入
		repPUR.getModel().getFSETTLEORGID().put("FNumber", fqzz);	// 结算组织
		repPUR.getModel().getFPAYORGID().put("FNumber", fqzz);	// 付款组织
		repPUR.getModel().setFSetAccountType(lzlx);	//  立账类型
		repPUR.getModel().setFAP_Remark(FinanceBean.delHTMLTag(FinanceBean.subRemark(fyzy)));	// 备注
		if(CustomUtil.isNotBlank(cgbm)) {
			repPUR.getModel().getFPURCHASEDEPTID().put("FNumber", cgbm);	// 采购部门
		}
		repPUR.getModel().setFCancelStatus("A");	// 作废状态，默认：A
		repPUR.getModel().setF_SZXY_OARequestID(requestInfo.getRequestManager().getRequestid()+"");	// OAID
		repPUR.getModel().setFsubHeadSuppiler(getFsubHeadSuppiler(gys));	// 表头供应商
		repPUR.getModel().setFsubHeadFinc(getFsubHeadFinc(mainFields));	// 表头财务
		
		// 明细表
		repPUR.getModel().setFEntityDetail(getFEntityDetail(requestInfo, mainFields));
		return repPUR;
	}
	
	/**
	 * 表头供应商
	 * @param requestInfo
	 * @param supplierNumber	供应商编码
	 * @return
	 */
	private FsubHeadSuppiler getFsubHeadSuppiler(String supplierNumber) {
		FsubHeadSuppiler fhs = new FsubHeadSuppiler();
		fhs.getFTRANSFERID().put("FNumber", supplierNumber);
		fhs.getFChargeId().put("FNumber", supplierNumber);
		fhs.getFORDERID().put("FNumber", supplierNumber);
		return fhs;
	}
	
	/**
	 * 表头财务
	 * @param requestInfo
	 * @param mainFields
	 * @return
	 */
	private FsubHeadFinc getFsubHeadFinc(Property[] mainFields) {
		String bb = super.getMainStringCK(mainFields, "bb", "币别不能为空！");	// 币别
		Double hl = super.getMainDouble(mainFields, "hl");	// 汇率
		FsubHeadFinc fhf = new FsubHeadFinc();
		fhf.getFMAINBOOKSTDCURRID().put("FNumber", "PRE001");	//  本位币，固定人民币
		if(!"PRE001".equals(bb)) {
			fhf.setFExchangeRate(hl);	// 汇率
		}
		fhf.getFEXCHANGETYPE().put("FNumber", "HLTX01_SYS");	// 汇率类型，固定值：HLTX01_SYS
		fhf.setFACCNTTIMEJUDGETIME(CustomUtil.getStringDate());	// 到期日计算日期
		return fhf;
	}

	/**
	 * 
	 * @param requestInfo
	 * @param mainFields
	 * @return
	 * @throws Exception
	 * @throws Exception
	 */
	public List<FEntityDetail> getFEntityDetail(RequestInfo requestInfo, Property[] mainFields) throws Exception, Exception {
		Integer requestid = requestInfo.getRequestManager().getRequestid();
		int formid = requestInfo.getRequestManager().getFormid();
		Map<String, Object> maxAmountMap = FinanceBean.getFYLFKMaxAmount(formid, "DT1", requestid);	// 查询当前单据最大金额
		String rowId = CustomUtil.getString(maxAmountMap.get("ID"));
		//Double hl = super.getMainDouble(mainFields, "hl");	// 汇率
		
		DetailTable  detailTable = requestInfo.getDetailTableInfo().getDetailTable(0);
		List<FEntityDetail> listFEntity = new ArrayList<FEntityDetail>();
		for(Row row: detailTable.getRow()) {
			String cplb = FinanceBean.getERPCplb(super.getCellString(row, "cplb"));	// 产品类别
			Double bhsje = super.getCellDoubleValue(row, "bhsje");	// 不含税金额
			Double se = super.getCellDoubleValue(row, "se");	// 税额
			
			String yfsm_new = super.getCellString(row, "yfsm_new");	//运费说明
			String fyxm = FinanceBean.getERPFyxm(super.getCellString(row, "fyxmbm"));	// 费用项目
			String fycdbm = FinanceBean.getERPDeptNumberV2(super.getCellString(row, "fycdbm"));	// 费用承担部门
			String ysfs = super.getCellString(row, "ysfs");	//运输方式
			
			Double jshj = super.getCellDoubleValue(row, "jshj");	// 价税合计
			String rkqj = super.getCellString(row, "rkqj");	// 费用内容(费用类必须填写)
			
			FEntityDetail detail = new FEntityDetail();
			detail.getFCOSTID().put("FNumber", fyxm);	// 费用项目编码
			detail.getFCOSTDEPARTMENTID().put("FNUMBER", fycdbm);	// 费用承担部门
			if(CustomUtil.isNotBlank(yfsm_new)) {
				detail.getF_SZXY_FREIGHTNOTE().put("FNumber", yfsm_new);	// 运费说明
			}
			if(CustomUtil.isNotBlank(ysfs)) {
				detail.getF_SZXY_YSFS().put("FNumber", ysfs);	// 运输方式
			}
			detail.setFComment(FinanceBean.subRemark(rkqj)); 	// 费用内容(费用类必须填写)
			
			// 发票费用明细
			if(CustomUtil.isNotBlank(rowId) && rowId.equals(row.getId())) {
				List<FEntityDetail> invoiceDetail = handlInvoiceDetail(requestInfo, detail);
				if(invoiceDetail.size() > 0) {
					// 若涉及发票的情况下，税额（传进项税的总和）字段在发票方法里面处理
					detail.setFNoTaxAmountFor_D(jshj-detail.getFTAXAMOUNTFOR_D());	// 不含税金额=价税合计-发票汇总进项税税额
					detail.setFALLAMOUNTFOR_D(jshj-detail.getFTAXAMOUNTFOR_D());	// 价税合计= 价税合计-发票汇总进项税税额
					detail.setFTAXAMOUNTFOR_D(0.0);	// 税额
				}else {
					// 若不涉及发票的情况下，原值不变
					detail.setFTAXAMOUNTFOR_D(0.0);	// 税额
					detail.setFNoTaxAmountFor_D(jshj);	// 不含税金额
					detail.setFALLAMOUNTFOR_D(jshj);	// 价税合计
				}
				
				listFEntity.add(detail);
				listFEntity.addAll(invoiceDetail);	// 将发票费用明细写入到ERP里面
			}else {
				detail.setFNoTaxAmountFor_D(jshj);	// 不含税金额
				detail.setFTAXAMOUNTFOR_D(0.0);	// 税额
				detail.setFALLAMOUNTFOR_D(jshj);	// 价税合计
				listFEntity.add(detail);
			}
			
		}
		return listFEntity;
	}
	
	/**
	 * 处理发票费用明细
	 * @param invoiceNumbers
	 * @param entry
	 * @param taxSum
	 * @return
	 * @throws Exception
	 */
	private List<FEntityDetail> handlInvoiceDetail(RequestInfo requestInfo, FEntityDetail detail) throws Exception {
		Double taxSum = 0.0;
		DetailTable  detailTable = requestInfo.getDetailTableInfo().getDetailTable(1);
		List<FEntityDetail> invoiceDetail = new ArrayList<FEntityDetail>();
		for(Row row: detailTable.getRow()) {
			String invoiceNumbers = super.getCellString(row, "fphm");
			List<CustomMap> invoiceMap = FinanceBean.getInvoiceData(invoiceNumbers);	// 发票号码
			if(CustomUtil.isNotBlank(invoiceMap)) {
				for(CustomMap map : invoiceMap) {
					Double tax = map.getDouble("DTAX");
					if(CustomUtil.isNotBlank(tax) && tax > 0) {
						FEntityDetail newDetail = new FEntityDetail();
						newDetail.getFCOSTID().put("FNumber", detail.getFCOSTID().get("FNumber"));	// 费用项目编码
						newDetail.getFCOSTDEPARTMENTID().put("FNUMBER", detail.getFCOSTDEPARTMENTID().get("FNUMBER"));	// 费用承担部门
						newDetail.setF_SZXY_Text1(map.getString("INVOICENUMBER"));	// 备注发票号
						if(CustomUtil.isNotBlank(detail.getF_SZXY_FREIGHTNOTE().get("FNumber"))) {
							newDetail.getF_SZXY_FREIGHTNOTE().put("FNumber", detail.getF_SZXY_FREIGHTNOTE().get("FNumber"));	// 运费说明
						}
						// 2023-10-31新增接口字段和逻辑
						String type = map.getString("INVOICETYPE");	// 进项税类型：0：税一；1：税二
						if("2".equals(type) ||  "20".equals(type) ||  "21".equals(type) || "32".equals(type) || "49".equals(type) ) {	// 为专票的进项税类型：0：税一；1：税二
							newDetail.setF_SZXY_INPUTTAXTYPE(0);		// 税一
						}else {
							newDetail.setF_SZXY_INPUTTAXTYPE(1);	// 税二
						}
						
						newDetail.setFComment(FinanceBean.subRemark(detail.getFComment()));	// 费用内容(费用类必须填写)
						newDetail.setFNoTaxAmountFor_D(null);	// 不含税金额
						newDetail.setFTAXAMOUNTFOR_D(tax);	// 税额
						newDetail.setFALLAMOUNTFOR_D(tax);	// 价税合计
						invoiceDetail.add(newDetail);
						taxSum = CustomUtil.add(taxSum, tax);	// 进项税累加
					}
					
				}
			}
		}
		
		detail.setFTAXAMOUNTFOR_D(taxSum);	// 赋值发票的可抵扣税额
		return invoiceDetail;
	}
	

	public static void setInputTax(FPAYBILLENTRY newEntry, String type) {
		
	}

}
